package by.sjc.grsu.web.controller;

import by.sjc.grsu.model.Account;
import by.sjc.grsu.model.Payment;
import by.sjc.grsu.model.Periodical;
import by.sjc.grsu.model.Subscription;
import by.sjc.grsu.security.CustomUserDetails;
import by.sjc.grsu.service.PeriodicalsService;
import by.sjc.grsu.service.SubscriptionsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Calendar;

@Controller
public class SubscribeController {

    public static final Logger logger = Logger.getLogger(SubscriptionsController.class);

    @Autowired
    private SubscriptionsService subscriptionsService;
    @Autowired
    private PeriodicalsService periodicalsService;



    @PreAuthorize("isFullyAuthenticated()")
    @RequestMapping(value = "/subs/subscribe")
    public String subscribe(
            HttpSession session,
            Authentication authentication,
            @RequestParam("id") int periodicalId ) {
        Periodical periodical = periodicalsService.getById(periodicalId);
        session.setAttribute("periodical", periodical);
        session.setAttribute("count", 1);

        return "/subscription/subscribeForm";
    }

    @PreAuthorize("isFullyAuthenticated()")
    @RequestMapping(value = "/subs/subscribeForm")
    public String subscribeForm(
            HttpSession session,
            @RequestParam("count") int count) {
        Periodical periodical = (Periodical) session.getAttribute("periodical");

        logger.info(periodical);
        if (null == periodical)
            return "redirect:/subs/list";

        session.setAttribute("count", count);
        return "redirect:/subs/payment";
    }

    @PreAuthorize("isFullyAuthenticated()")
    @RequestMapping(value = "/subs/payment", method = RequestMethod.GET)
    public ModelAndView payment(HttpSession session) {
        Periodical periodical = (Periodical) session.getAttribute("periodical");
        Integer cnt = (Integer) session.getAttribute("count");
        if (null == cnt) cnt =1;

        logger.info(periodical);
        if (null == periodical)
            return new ModelAndView("redirect:/subs/list");

        ModelAndView mav = new ModelAndView("/subscription/payment");
        Payment payment = new Payment();
        payment.setSum( subscriptionsService.getSubscribeCost(periodical.getId(), cnt) );
        logger.info("putted payment="+payment);
        mav.addObject("payment", payment);
        return mav;
    }

    @PreAuthorize("isFullyAuthenticated()")
    @RequestMapping(value = "/subs/finalSubscribe")
    public ModelAndView payment(
            @Valid Payment payment,
            HttpSession session,
            Authentication authentication) {
        Periodical periodical = (Periodical) session.getAttribute("periodical");
        Integer cnt = (Integer) session.getAttribute("count");
        Account account = ((CustomUserDetails) authentication.getPrincipal()).getAccount();

        if (null == payment || null == periodical || null == cnt)
            return new ModelAndView("redirect:/subs/list");

        payment.setSum( subscriptionsService.getSubscribeCost(periodical.getId(), cnt) );

        logger.info("finalSubscribe=\nacc=" + account + "\npayment=" + payment + "\npeiodical=" + periodical + "\ncnt=" + cnt);
        subscriptionsService.subscribe(account.getId(), periodical.getId(), cnt);
        subscriptionsService.registerPayment(payment);

        ModelAndView mav = new ModelAndView("redirect:/subs/list");
        mav.addObject("payment", new Payment());
        return mav;
    }

}
